package com.jiwei.community.utils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @author 季伟
 * 功能：采用DFA算法实现敏感词的匹配和替换
 */
public class SensitiveWordUtils {
    public static DfaNode root;
    static {
        root = buildDfa("习近平","共产党","中华民族","党","党中央");
    }

    /**
     * 为多个敏感词建立dfa
     * @param senseList
     * @return
     */
    public static DfaNode buildDfa(String... senseList){
        DfaNode dfa = new DfaNode();
        for (String str : senseList){
            addSensitiveWord(str,dfa);
        }
        return dfa;
    }

    /**
     * 为多个敏感词建立dfa
     * @param words
     * @return
     */
    public static DfaNode buildDfa(List<String> words){
        DfaNode dfa = new DfaNode();
        for (String str : words){
            addSensitiveWord(str,dfa);
        }
        return dfa;
    }

    /**
     * 添加屏蔽词
     * @param word 屏蔽词
     * @param root 屏蔽词树
     */
    public static void addSensitiveWord(String word,DfaNode root){
        DfaNode curr = root;
        for (int i =0;i<word.length();i++){
            DfaNode next = null;
            if( (next = curr.getChildNode(word.charAt(i))) == null){
                next = new DfaNode();
                if (i == word.length()-1)next.setEnd(true);
                curr.setChildNode(word.charAt(i), next);
            }
            curr = next;
        }
    }
    /**
     * 根据屏蔽词树替换文本的内容为*
     * @param word
     * @param root
     * @return
     */
    public static String replaceWord(String word,DfaNode root){
        if(word == null)return "";
        StringBuilder originWord = new StringBuilder(word);
        DfaNode curr = root;
        for(int i = 0;i<originWord.length();i++){
            curr = root.getChildNode(originWord.charAt(i));
            int j =i;
            StringBuilder replaceStr = new StringBuilder();
            while (curr != null){
                replaceStr.append("*");
                if (curr.isEnd()){
                    originWord.replace(i,j+1, replaceStr.toString());
                    break;
                }
                j++;
                curr = curr.getChildNode(originWord.charAt(j));
            }
            i = j;
        }
        return originWord.toString();
    }

    /**
     * 根据屏蔽词树替换文本的内容为*
     * @param word
     * @return
     */
    public static String replaceWord(String word){
        return replaceWord(word,root);
    }

    public static String getReplaceStr(int len){
        String str = "";
        for (int i =0;i<len;i++){
            str += '*';
        }
        return str;
    }

}
class DfaNode{
    private boolean isEnd = false;
    private Map<Character,DfaNode> children = new HashMap<>();
    public Map<Character, DfaNode> getChildren() {
        return children;
    }
    public void setChildNode(Character key, DfaNode node){
        children.put(key,node);
    }
    public DfaNode getChildNode(Character key){
        return children.get(key);
    }
    public boolean isEnd() {
        return isEnd;
    }
    public void setEnd(boolean end) {
        isEnd = end;
    }
    @Override
    public String toString() {
        return "{" +
                "isEnd=" + isEnd +
                ", children=" + children +
                '}';
    }
}
